const mongoose = require('mongoose');

const positionSchema = new mongoose.Schema({
  // 基本信息
  positionCode: {
    type: String,
    required: true,
    index: true
  },
  positionName: {
    type: String,
    required: true
  },
  recruitmentNumber: {
    type: Number,
    default: 1
  },
  
  // 要求信息
  education: String,
  degree: String,
  major: String,
  experienceRequirement: String,
  essayCategory: String,
  professionalSubject: String,
  sex: String,
  age: String,
  
  // 机构信息
  recruitingOrganization: String,
  organizationNature: String,
  organizationLevel: String,
  positionCategory: String,
  positionLevel: String,
  
  // 其他信息
  otherRequirements: String,
  remarks: String,
  contactPhone: String,
  
  // 元数据
  province: {
    type: String,
    required: true
  },
  city: {
    type: String,
    required: true
  },
  date: {
    type: String,
    required: true
  },
  
  // 时间戳
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

// 添加复合索引以提高查询性能
positionSchema.index({ province: 1, city: 1, date: 1 });
positionSchema.index({ positionCode: 1, province: 1, city: 1, date: 1 });

// 在保存前更新 updatedAt 字段
positionSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

module.exports = mongoose.model('Position', positionSchema);